//
//  KHUICVCell_HomeCategory.m
//  EduCourse
//
//  Created by yanyan on 2017/8/9.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUITVCell_HomeAllCategory.h"
#import "CYUICVDSD_base.h"
#import "CYUICVDSDBridge.h"
#import "ECData_Format.h"
#import "ECUICVCell_HomeCategory.h"
@interface ECUITVCell_HomeAllCategory()
{
    UICollectionView *menuView;
    CYUICVDSD_base *dsd;

}
@end
@implementation ECUITVCell_HomeAllCategory
- (void)addSubviews
{
    self.contentView.backgroundColor = [UIColor colorWithRed:0.9686 green:0.9686 blue:0.9686 alpha:1];
    CGRect cvFrame = CGRectMake(0, 0, kScreenWidth, 70) ;
    UICollectionViewFlowLayout *cvLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemSpacing = 13 * kPercentage;
    cvLayout.itemSize = CGSizeMake(floor((CGRectGetWidth(cvFrame)-itemSpacing*5)/5.0),  (CGRectGetHeight(cvFrame)-10));
    cvLayout.sectionInset = UIEdgeInsetsMake(itemSpacing/2, itemSpacing/2, 0, itemSpacing/2);
    //The minimum spacing to use between lines of items in the grid.
    cvLayout.minimumLineSpacing = 5;
    //The minimum spacing to use between items in the same row.
    cvLayout.minimumInteritemSpacing = itemSpacing/2;
    menuView = [[UICollectionView alloc]initWithFrame:cvFrame collectionViewLayout:cvLayout];
//    menuView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    menuView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:menuView];
    dsd = [[CYUICVDSD_base alloc]init];
    dsd.classesByCellReuseId = @{@"menuCell":[ECUICVCell_HomeCategory class]};
    
    [CYUICVDSDBridge bridgeView:menuView dsd:dsd];
    WeakSelf(weakSelf);
    dsd.block_itemSelected = ^(id obj) {
        if (weakSelf.block_selected) {
            weakSelf.block_selected(obj);
        }
    };
}

-(void)setObject:(id)object
{
    [super setObject:object];
    dsd.object = @{kCollectionCellDataSourceId :@[object]};
    [menuView reloadData];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
@interface ECUIView_HomeAllCategory()
{
    UICollectionView *menuView;
    CYUICVDSD_base *dsd;
    
}
@end

@implementation ECUIView_HomeAllCategory
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        CGRect cvFrame = CGRectMake(0, 0, kScreenWidth, 70) ;
        UICollectionViewFlowLayout *cvLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat itemSpacing = 13 * kPercentage;
        cvLayout.itemSize = CGSizeMake((CGRectGetWidth(cvFrame)-itemSpacing*5)/5,  (CGRectGetHeight(cvFrame)-10));
        cvLayout.sectionInset = UIEdgeInsetsMake(5, itemSpacing/2, 0, itemSpacing/2);
        //The minimum spacing to use between lines of items in the grid.
        cvLayout.minimumLineSpacing = 5;
        //The minimum spacing to use between items in the same row.
        cvLayout.minimumInteritemSpacing = itemSpacing/2;
        menuView = [[UICollectionView alloc]initWithFrame:cvFrame collectionViewLayout:cvLayout];
        menuView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        menuView.exclusiveTouch = YES;
        menuView.backgroundColor = [UIColor whiteColor];
        [self addSubview:menuView];
        dsd = [[CYUICVDSD_base alloc]init];
        dsd.classesByCellReuseId = @{@"menuCell":[ECUICVCell_HomeCategory class]};
        WeakSelf(weakSelf);
        dsd.block_itemSelected = ^(id obj) {
            if (weakSelf.block_selected) {
                weakSelf.block_selected(obj);
            }
        };
        [CYUICVDSDBridge bridgeView:menuView dsd:dsd];
       
    }
    return self;
}
-(void)setObject:(id)object
{
    [super setObject:object];
    dsd.object = @{kCollectionCellDataSourceId :@[object]};
    [menuView reloadData];
    
}
@end
